package com.neuedu.api.collection;

import java.util.*;

/**
 * @author 金山
 * 项目：javase
 * site: https://blog.fulfill.com.cn
 * 描述
 * @data 2024/10/2514:40
 */
public class MyHashSet {

    public static void main(String[] args) {



        //创建集合对象
        //无序，不可重复
        // Set set = new HashSet();
        Set set = new HashSet(100);




        //添加元素
        set.add(100);
        set.add(200);
        set.add(300);
        set.add(400);
        set.add(100);
        set.add(null);
        set.add(null);

        System.out.println("set.size() = " + set.size());


        List list = Arrays.asList(100,200,300,100,200);
        Set set2 = new HashSet(list);  //size   3
        System.out.println("set2.size() = " + set2.size());

        // 不能使用 index 获取元素
        // set.get(i)

        //遍历元素
        for (Iterator it = set.iterator(); it.hasNext(); ) {
            Object item = it.next();
            System.out.println("item = " + item);
        }



        Set set3 = new HashSet();
        set3.addAll(list);
        System.out.println("set3 = " + set3);


        Set set4 = new HashSet(   Arrays.asList(10,20,30)    );
        System.out.println("set4 = " + set4);
        System.out.println("set4.remove(20) = " + set4.remove(20));
        System.out.println("set4 = " + set4);



        List list2 = new ArrayList(   Arrays.asList(10,20,30)    );
        System.out.println("list2 = " + list2);
        // System.out.println("list2.remove(20) = " + list2.remove(20));   //int index
        System.out.println("list2.remove(new Integer(20)) = " + list2.remove(new Integer(20)));   //Object obj
        System.out.println("list2 = " + list2);

        Set set5 = new HashSet();
        set5.add(100);
        set5.add(300);
        set5.add(200);
        set5.add("200");


        // Object[] objects = set5.toArray();
        for (Object o : set5) {
            System.out.println("o = " + o+", class:"+o.getClass());
        }

        TreeSet treeSet = new TreeSet();


        treeSet.add(300);
        treeSet.add(100);
        treeSet.add(200);

        System.out.println("treeSet = " + treeSet);


    }
}
